<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/30
 * Time: 15:29
 */

class LocalOrder extends MY_Controller{
    function __construct() {
        parent::__construct();
        $this->load->model('LocalOrder/local_order_model','localOrder');
    }

    /**
     * 本地单
     */
    public function index(){
        //获取站点
        $data['siteList']=$this->localOrder->GetSite();
        //获取维修类型
        $data['serviceType']=$this->localOrder->GetServiceType();
        //获取平台客户
        $data['clientList']=$this->localOrder->GetClient(1);
        //获取产品类型
        $data['productType']=$this->localOrder->GetOMSProductType();
        //生成来源单号
        $data['source_oid']=uniqid('OM');
        //生成订单号
        $data['orderId']=build_order_no();
        $this->load->view('defaults/localOrder/local_index',$data);
    }

    /**
     * 新增本地单
     */
    public function AddLocalOrder(){
        $gp=gp();
        //产品数量
        $product_quantity=count($gp['productSku']);
        $total=0;
        for($m=0;$m<$product_quantity;$m++){
            $total+=$gp['quantity'][$m];
        }
        //订单信息
        $service_center=explode('_',$gp['service_center']);
        $orderData=array(
            'order_num'=>$gp['order_num'],
            'create_time'=>date('Y-m-d H:i:s',time()),
            'add_time'=>time(),
            'add_user'=>$this->users['name'],
            'technician'=>$this->users['name'],
            'service_center'=>$service_center[0],
            'point'=>$service_center[1],
            'service_type'=>$gp['service_type'],
            'storeOrShip'=>$gp['storeOrShip'],
            'grandTotal'=>$gp['grandTotal'],
            'currency_code'=>$service_center[2],
            'source_oid'=>$gp['source_oid'],
            'delivery_address'=>$gp['delivery_street_address'].','.$gp['delivery_city'].','.$gp['delivery_state'].','.$gp['delivery_country'].','.$gp['delivery_zipcode'],
            'pickup_address'=>$gp['delivery_street_address'].','.$gp['delivery_city'].','.$gp['delivery_state'].','.$gp['delivery_country'].','.$gp['delivery_zipcode'],
            'product_quantity'=>$total,
        );
        //保存客户信息 并且生成客户编号
        if($gp['consumer_name'] && $gp['consumer_email']) {
            //客户信息
            $clientInfo = array(
                'name' => $gp['consumer_name'],
                'type'=>3,
                'name_en' => $gp['consumer_name'],
                'email' => $gp['consumer_email'],
                'tel' => $gp['consumer_phone'],
            );
            $consumer_num=$this->localOrder->AddClientInfo($clientInfo);
            $orderData['consumer_num']=$consumer_num;
        }
        //平台
        if($gp['merchant_num']){
            $orderData['merchant_num']=$gp['merchant_num'];
        }
        //订单新增散客信息
        $orderData['consumer_name']=$gp['consumer_name'];
        $orderData['delivery_street_address']=$gp['delivery_street_address'];
        $orderData['delivery_city']=$gp['delivery_city'];
        $orderData['delivery_state']=$gp['delivery_state'];
        $orderData['delivery_country']=$gp['delivery_country'];
        $orderData['delivery_zipcode']=$gp['delivery_zipcode'];
        $orderData['consumer_phone']=$gp['consumer_phone'];
        $orderData['consumer_email']=$gp['consumer_email'];

        //添加订单信息

        if($gp['batch_operations']) {
            $fieldsList = $this->db->list_fields('mt_multi_orders');
            foreach ($orderData as $key => $val) {
                if (!in_array($key, $fieldsList)) continue;
                $orderInsertData[$key] = trim($val);
            }
            $orderInsertData['quantity']=$total;
            $orderId = $this->common_model->insert('mt_multi_orders',$orderInsertData);
        }else{
            $orderId = $this->localOrder->AddLocalOrderInfo($orderData);
        }

        //添加产品信息
        if($orderId){
            //获取产品数量
            //循环产品信息
            for($i=0;$i<$product_quantity;$i++){
                $brandInfo=explode('_',$gp['brand_id'][$i]);
                $modelInfo=explode('_',$gp['model_id'][$i]);
                $bid=$brandInfo[0];
                $mid=$modelInfo[0];
                $brandName=$brandInfo[1];
                $modelName=$modelInfo[1];
                //对类型做出转换
                $category_id=$this->TypeConversion($gp['category_id'][$i]);
                //获取品牌客户Id
                $brandCustomerNum=$this->localOrder->GetBrandCustomerNum($bid);
                for($j=0;$j<$gp['quantity'][$i];$j++) {
                    //产品信息
                    $productData = array(
                        'oid' => $orderId,
                        'order_num' => $gp['order_num'],
                        'brandCustomerNum' => $brandCustomerNum,
                        'category_id' => $category_id,
                        'brand_id' => $bid,
                        'product_brand' => $brandName,
                        'model_id' => $mid,
                        'type_id' => $gp['category_id'][$i],
                        'product_model' => $modelName,
                        'productSku' => $gp['productSku'][$i],
                        'quantity'=>1,
                        'customerRemark' => $gp['customerRemark'][$i],
                        'imei' => $gp['imei'][$i],
                    );
                    //添加产品信息
                    if($gp['batch_operations']) {
                        $fieldsList = $this->db->list_fields('mt_multi_products');
                        foreach ($productData as $key => $val) {
                            if (!in_array($key, $fieldsList)) continue;
                            $productInsertData[$key] = trim($val);
                        }
                        $this->common_model->insert('mt_multi_products',$productInsertData);
                    }else{
                        $this->localOrder->AddProductInfo($productData);
                    }
                }
            }
            redirect(blang_url('orders/Order_new'));
        }
    }

    /**
     * 类型转换
     * @param $custom_id
     * @return mixed
     */
    public function TypeConversion($custom_id){
        $data=$this->common_model->selectFirst('mt_product_category',array('sap_id'=>$custom_id));
        if($data){
            return $data['id'];
        }else{
            return $custom_id;
        }
    }
}